package com.tbit.uqbike.client.business.captcha.config;

import com.anji.captcha.model.common.CaptchaTypeEnum;
import com.anji.captcha.model.common.Const;
import com.anji.captcha.service.CaptchaCacheService;
import com.anji.captcha.service.CaptchaService;
import com.anji.captcha.service.impl.CaptchaServiceFactory;
import com.tbit.uqbike.client.business.captcha.service.CaptchaCacheServiceRedisImpl;
import org.springframework.boot.autoconfigure.cache.CacheType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Properties;

/**
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2024/12/10
 */
@Configuration
public class AjCaptchaServiceConfig {
    /**
     * 验证码服务
     */
    @Bean
    public CaptchaService captchaService() {
        Properties config = new Properties();
        config.put(Const.CAPTCHA_CACHETYPE, "redis");
        config.put(Const.CAPTCHA_TYPE, CaptchaTypeEnum.BLOCKPUZZLE.getCodeValue());
        config.put(Const.CAPTCHA_INTERFERENCE_OPTIONS, "0");
        config.put(Const.CAPTCHA_SLIP_OFFSET, "5");
        config.put(Const.CAPTCHA_AES_STATUS, "true");
        config.put(Const.CAPTCHA_WATER_MARK, "");

        // 限流
        config.put(Const.REQ_FREQUENCY_LIMIT_ENABLE, "true");
        config.put(Const.REQ_GET_MINUTE_LIMIT, "20");

        return CaptchaServiceFactory.getInstance(config);
    }

    /**
     * 验证码缓存redis实现
     */
    @Bean(name = "AjCaptchaCacheService")
    @Primary
    public CaptchaCacheService captchaCacheService(StringRedisTemplate redisTemplate){
        CaptchaCacheService ret = CaptchaServiceFactory.getCache("redis");
        if(ret instanceof CaptchaCacheServiceRedisImpl){
            ((CaptchaCacheServiceRedisImpl)ret).setStringRedisTemplate(redisTemplate);
        }
        return ret;
    }
}
